package risk.client;

import java.awt.Color;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import risk.client.gui.GameBoard;
import risk.client.gui.GameFrame;
import risk.client.gui.MapEditor;
import risk.client.gui.SetUpGameDialog;
import risk.client.logic.GameManager;

/**
 * @author: Johan
 */
public class Risk implements Runnable {    
    public final int WINDOW_HEIGHT  = 600;
    public final int WINDOW_WIDTH   = 1000;
    public static final int MODE_NEWGAME = 0;
    public static final int MODE_NEWMAP = 1;
    public static int COMPUTER_SLEEP = 500;
    private boolean running;
    
    public int mode;
    
    public GameBoard board;
    public GameFrame frame;
    private GameManager manager;
    
    /**
     * Constructor
     * 
     * @author johan
     */
    public Risk(int mode, SetUpGameDialog gd) {
        this.mode = mode;
        String map = null;
        
        // If mode is new game
        if(mode == MODE_NEWGAME) {
            map = JOptionPane.showInputDialog(null,  "Open map::",  "Open map",  JOptionPane.QUESTION_MESSAGE, null, null, "risk").toString();
                          
            while(!GameBoard.mapExists(map)) {
                map = JOptionPane.showInputDialog(null,  "Open map:",  "Open map",  JOptionPane.QUESTION_MESSAGE);
            }
        }
        
        
            board = new GameBoard(map, this);
            frame = new GameFrame(board, this);
            manager = new GameManager(this); 
            
        if(mode == MODE_NEWGAME) {
            addPlayers(gd);
            frame.showPanelInfo();
        }
    }
    
    /**
     * @author: Johan
     * @param gd
     */
    public void addPlayers(SetUpGameDialog gd) {
        manager.addPlayer(gd.getPlayer1Type(), gd.getPlayer1Name(), Color.RED);
        manager.addPlayer(gd.getPlayer2Type(), gd.getPlayer2Name(), Color.GREEN);
    }

    @Override
    public void run() {
        setRunning(true);
        
        if(mode == MODE_NEWGAME)
            manager.startManager();
        else {
            MapEditor mapEditor = new MapEditor(this);
            mapEditor.startMapEditor();
        }

    }
    
    public boolean running() {
        return running;
    }
    
    public void setRunning(boolean running) {
        this.running = running;
    }
}
